import 'package:flutter/material.dart';
import 'package:flutter_module/base/base.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 官方原生状态管理 setState({})

/// 无状态 控件
// class Counter extends StatelessWidget {
//   final int count;
//
//   const Counter({super.key, required this.count});
//
//   @override
//   StatelessElement createElement() {
//     print("创建");
//     return super.createElement();
//   }
//
//   @override
//   Widget build(BuildContext context) {
//     print("构建");
//     return Text(
//       "$count",
//       style: TextStyle(color: Color(0xFF333333), fontSize: 24.w),
//     );
//   }
// }

/// ViewModel + Model
class VmPageNative {
  int count = 0;

  // 数据更新
  void upCount() {
    count++;
  }
}

/// View
class PageNative extends BaseStateWidget {
  const PageNative({super.key});

  @override
  StatefulElement createElement() {
    print("构建 element");
    return super.createElement();
  }

  @override
  PageNativeState buildState() => PageNativeState();
}

/// View State
class PageNativeState extends BaseState<PageNative> {
  // VM 数据都存入
  VmPageNative vmPageNative = VmPageNative();

  // 点击事件
  void clickUp() {
    // 强制刷新
    setState(() {
      vmPageNative.upCount();
    });
  }

  @override
  Widget setContent(BuildContext context) {
    print("构建 widget");
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text(
          "NativeState",
          style: TextStyle(color: Colors.black, fontSize: 36.w),
        ),
        Expanded(child: SizedBox()),
        // 数据显示
        Text(
          "${vmPageNative.count}",
          style: TextStyle(color: Color(0xFF333333), fontSize: 24.w),
        ),
        TextButton(
          onPressed: clickUp,
          child: Text("Count ++", style: TextStyle(fontSize: 24.w)),
        ),
        Expanded(child: SizedBox()),
      ],
    );
  }
}
